package com.wzx.demo02;

import com.wzx.domain.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;


public class Demo02 {
    public static void main(String[] args) {
        Person p = new Person("jack","123456");
        //生成代理类，创建该类对象
        PersonDao2 personDao2 = new PersonDao2();
        Logger logger = LoggerFactory.getLogger(PersonDao2.class);
        ClassLoader classLoader=PersonDao2.class.getClassLoader();//与原来类一样
        Class<?>[] interfaces=PersonDao2.class.getInterfaces();//与原来类一样
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                调用Dao类的update(person)
//                调用Logger类的debug(“update  parameter return”)
                //update delete add 的执行
                //开始
                long start = System.currentTimeMillis();
                Object returnVal = method.invoke(personDao2,args);
                long time = System.currentTimeMillis()-start;
                logger.debug("方法名:"+method.getName()+" 参数"+ Arrays.toString(args)+" 返回值:"+returnVal+" 耗时"+time);
                return returnVal;
            }
        };
        IPersonDao2 personDao= (IPersonDao2) Proxy.newProxyInstance(classLoader,interfaces,handler);
 //       personDao.add(p);
        personDao.update(p);
//        personDao.delete(1);
    }
}
